<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 头部布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="guide" /> <span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepartment('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体布局 -->
        <!-- default-expand-all 默认展开所有子节点 -->
        <el-tree :data="list" :props="defaultProps" default-expand-all>
          <!-- 作用域插槽 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="guide" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepartment(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="delDepartment(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 添加和编辑的 对话框 -->
    <el-dialog
      v-if="showDialog"
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <DeptDialog :origin-list="originList" :is-edit="isEdit" :pid="pid" @close="showDialog=false" @success="hSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { arrTotree } from '@/utils'
import DeptDialog from './deptDialog.vue'
export default {
  components: { DeptDialog },
  data() {
    return {
      originList: [], // 用于子组件校验
      isEdit: null, // 是否是编辑
      pid: '', // 当前选中的部门id
      // 控制对话框的显示和隐藏
      showDialog: false,
      // 树形数据
      list: [],
      defaultProps: {
        children: 'children',
        label: 'label' // 部门
      }
    }
  },
  created() {
    this.initDepartments()
  },
  methods: {
    hSuccess() {
      // 关闭弹窗
      this.showDialog = false
      // 刷新部门信息
      this.initDepartments()
    },
    async del(id) {
      await delDepartment(id)
      // 刷新部门信息
      this.initDepartments()
      this.$message.success('删除部门成功')
    },
    // 删除部门
    delDepartment(id) {
      this.$confirm('确定要删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        try {
          this.del(id)
        } catch (err) {
          alert(err)
        }
      }).catch(() => {})
    },
    // 编辑部门
    editDepartment(id) {
      // 更新id
      this.pid = id
      // 设置标题为 编辑
      this.isEdit = true
      this.showDialog = true
    },
    // 添加部门
    addDepartment(id) {
      // 打开弹窗
      this.showDialog = true
      // 拿到当前部门id， 传给子
      // console.log(id)
      this.pid = id
      // 设置标题为 添加
      this.isEdit = false
    },
    // 获取部门信息
    async initDepartments() {
      const { data: res } = await getDepartments()
      console.log(res)
      res.depts.shift()
      // 抽取 需要给子组件做检验的数据
      this.originList = res.depts.map(item => {
        return {
          id: item.id,
          pid: item.pid,
          code: item.code,
          name: item.name
        }
      })
      // 数组转树
      this.list = arrTotree(res.depts)
    }
  }
}
</script>
